
package com.kwedding.service.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.kwedding.bean.TB_Brand;
import com.kwedding.bean.TB_BrandSorts;
import com.kwedding.bean.TB_City;
import com.kwedding.dao.TB_BrandMapper;
import com.kwedding.dao.TB_BrandSortsMapper;
import com.kwedding.dao.TB_CityMapper;
import com.kwedding.dto.web.RequestWebAddBrand;
import com.kwedding.dto.web.RequestWebAddBrandSorts;
import com.kwedding.dto.web.RequestWebBrandSimple;
import com.kwedding.dto.web.RequestWebGetBrandList;
import com.kwedding.dto.web.RequestWebGetBrandSortsList;
import com.kwedding.dto.web.RequestWebUpDownBrand;
import com.kwedding.dto.web.RequestWebUpDownBrandStrots;
import com.kwedding.dto.web.RequestWebUpdateBrand;
import com.kwedding.dto.web.RequestWebUpdateBrandSorts;
import com.kwedding.dto.web.ResponseBrandSortsSimple;
import com.kwedding.dto.web.ResponseWebBrandList;
import com.kwedding.dto.web.ResponseWebBrandSimple;
import com.kwedding.dto.web.ResponseWebBrandSortsList;
import com.kwedding.util.CheckedUtil;
import com.kwedding.util.Constants;
import com.kwedding.util.CopyPageInfo;
import com.kwedding.util.CopyerObject;
import com.kwedding.util.IDGenerate;
import com.kwedding.util.LoggerParent;
import com.kwedding.util.RequestErrorBackException;
import com.kwedding.util.Resp_PageInfo;

@Service
public class WebBrandService extends LoggerParent {

	@Autowired
	private TB_BrandSortsMapper tb_BrandSortsMapper;
	@Autowired
	private TB_BrandMapper tb_BrandMapper;
	@Autowired
	private TB_CityMapper tb_CityMapper;

	/**
	 * 添加品牌分类
	 * 
	 * @param addBrandSorts
	 */
	@Transactional
	public void insertBrandSorts(RequestWebAddBrandSorts addBrandSorts) {
		CheckedUtil.isNull(addBrandSorts.getName(), Constants.REQUEST_ERROR_1, "分类名称不能为空");
		TB_BrandSorts tb_BrandSorts = tb_BrandSortsMapper.selectByName(addBrandSorts.getName());
		if (tb_BrandSorts != null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_2, "该分类已经存在且使用");
		}
		tb_BrandSorts = CopyerObject.copyerObject(addBrandSorts, TB_BrandSorts.class);
		tb_BrandSorts.setCreateTime(new Date());
		tb_BrandSorts.setSortsId(IDGenerate.generate());
		tb_BrandSorts.setStatus(false);
		tb_BrandSorts.setUpdateTime(new Date());
		if (tb_BrandSortsMapper.insert(tb_BrandSorts) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_3, "添加商品分类失败");
		}

	}

	/**
	 * 获取品牌分类列表
	 * 
	 * @param brandSortsList
	 */
	@Transactional(readOnly = true)
	public Resp_PageInfo<ResponseWebBrandSortsList> getBrandSortsList(RequestWebGetBrandSortsList getBrandSortsList) {
		int page = 1;
		if (getBrandSortsList.getPageNum() != null) {
			page = getBrandSortsList.getPageNum();
		}
		PageHelper.startPage(page, Constants.PAGESIZE);
		List<TB_BrandSorts> tb_BrandSorts = tb_BrandSortsMapper.getBrandSortsList(getBrandSortsList.getStatus(),
				getBrandSortsList.getSearchName());
		List<ResponseWebBrandSortsList> brandSortsLists = new ArrayList<>();
		if (tb_BrandSorts != null && tb_BrandSorts.size() > 0) {
			tb_BrandSorts.stream().forEach(r -> {
				ResponseWebBrandSortsList brandSortsList = CopyerObject.copyerObject(r,
						ResponseWebBrandSortsList.class);
				brandSortsList.setIdStr(r.getSortsId().toString());
				brandSortsLists.add(brandSortsList);
			});
		}
		return CopyPageInfo.copyPageInfo(tb_BrandSorts, brandSortsLists);
	}

	/**
	 * 修改品牌分类
	 * 
	 * @param requestWebUpdateBrandSorts
	 */
	public void updateBrandSorts(RequestWebUpdateBrandSorts requestWebUpdateBrandSorts) {
		CheckedUtil.isNull(requestWebUpdateBrandSorts.getSortsId(), Constants.REQUEST_ERROR_1, "请选择要修改的内容");
		CheckedUtil.isNull(requestWebUpdateBrandSorts.getName(), Constants.REQUEST_ERROR_2, "名称不能为空");
		TB_BrandSorts tb_BrandSorts = tb_BrandSortsMapper.selectByPrimaryKey(requestWebUpdateBrandSorts.getSortsId());
		if (tb_BrandSorts == null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_3, "该分类不存在或者已经删除");
		}
		TB_BrandSorts tb_BrandSorts1 = tb_BrandSortsMapper.selectByName(requestWebUpdateBrandSorts.getName());
		if (tb_BrandSorts1 != null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_3, "该分类已经存在且使用");
		}
		tb_BrandSorts1 = CopyerObject.copyerObject(requestWebUpdateBrandSorts, TB_BrandSorts.class);
		tb_BrandSorts1.setUpdateTime(new Date());
		if (tb_BrandSortsMapper.updateSelectivebyPrimaryKey(tb_BrandSorts1) < 1) {

		}
	}

	/**
	 * 上下架品牌分类
	 * 
	 * @param upDownBrandStrots
	 */
	@Transactional
	public void upDownBrandSortsSatus(RequestWebUpDownBrandStrots upDownBrandStrots) {
		CheckedUtil.isNull(upDownBrandStrots.getSortsId(), Constants.REQUEST_ERROR_1, "请选择要修改的内容");
		TB_BrandSorts tb_BrandSorts = tb_BrandSortsMapper.selectByPrimaryKey(upDownBrandStrots.getSortsId());
		if (tb_BrandSorts == null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_2, "内容不存在或已删除");
		}
		tb_BrandSorts.setStatus(!tb_BrandSorts.getStatus());
		tb_BrandSorts.setUpdateTime(new Date());
		if (tb_BrandSortsMapper.updateSelectivebyPrimaryKey(tb_BrandSorts) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_2, "上下架品牌分类失败");
		}
	}

	/**
	 * 添加品牌
	 * 
	 * @param addBrand
	 */
	@Transactional
	public void insertBrand(RequestWebAddBrand addBrand) {
		CheckedUtil.isNull(addBrand.getSortsId(), Constants.REQUEST_ERROR_1, "分类不能为空");
		CheckedUtil.isNull(addBrand.getBrandName(), Constants.REQUEST_ERROR_2, "品牌名称不能为空");
		CheckedUtil.isNull(addBrand.getCityId(), Constants.REQUEST_ERROR_3, "地区能为空");
		TB_Brand tb_Brand = tb_BrandMapper.selectByNameAndCityId(addBrand.getCityId(), addBrand.getBrandName());
		if (tb_Brand != null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_4, "该地区品牌已经存在");
		}
		tb_Brand = CopyerObject.copyerObject(addBrand, TB_Brand.class);
		tb_Brand.setBrandId(IDGenerate.generate());
		tb_Brand.setStatus(false);
		tb_Brand.setCreateTime(new Date());
		tb_Brand.setUpdateTime(new Date());
		if (tb_BrandMapper.insert(tb_Brand) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_5, "添加品牌信息失败");
		}
	}

	/**
	 * 获取品牌列表
	 * 
	 * @param getBrandList
	 * @return
	 */
	@Transactional(readOnly = true)
	public Resp_PageInfo<ResponseWebBrandList> getBrandList(RequestWebGetBrandList getBrandList) {
		int page = 1;
		if (getBrandList.getPageNum() != null) {
			page = getBrandList.getPageNum();
		}
		PageHelper.startPage(page, Constants.PAGESIZE);
		List<TB_Brand> tb_Brands = tb_BrandMapper.selectBrandList(getBrandList.getStatus(),
				getBrandList.getSearchName(), getBrandList.getCityCode());
		List<ResponseWebBrandList> webBrandLists = new ArrayList<>();
		tb_Brands.stream().forEach(r -> {
			ResponseWebBrandList responseWebBrandList = CopyerObject.copyerObject(r, ResponseWebBrandList.class);
			responseWebBrandList.setIdStr(r.getBrandId().toString());
			responseWebBrandList.setSortsId(r.getSortsId().toString());
			responseWebBrandList.setImages(r.getImage());
			// 设置品牌分类信息
			TB_BrandSorts tb_BrandSorts = tb_BrandSortsMapper.selectByPrimaryKey(r.getSortsId());
			if (tb_BrandSorts != null && tb_BrandSorts.getStatus() == false) {
				responseWebBrandList.setSortsName(tb_BrandSorts.getName());
			}
			webBrandLists.add(responseWebBrandList);
		});
		return CopyPageInfo.copyPageInfo(tb_Brands, webBrandLists);

	}

	/**
	 * 修改品牌信息
	 * 
	 * @param updateBrand
	 */
	@Transactional
	public void updateBrand(RequestWebUpdateBrand updateBrand) {
		CheckedUtil.isNull(updateBrand.getBrandId(), Constants.REQUEST_ERROR_1, "请先选择要修改的品牌信息");
		CheckedUtil.isNull(updateBrand.getSortsId(), Constants.REQUEST_ERROR_1, "分类不能为空");
		CheckedUtil.isNull(updateBrand.getBrandName(), Constants.REQUEST_ERROR_2, "品牌名称不能为空");
		CheckedUtil.isNull(updateBrand.getCityId(), Constants.REQUEST_ERROR_3, "地区能为空");
		TB_Brand tb_Brand1 = tb_BrandMapper.selectByPrimaryKey(updateBrand.getBrandId());
		if (tb_Brand1 == null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_4, "要修改的品牌信息不存在或者已删除");
		}
		TB_Brand tb_Brand2 = tb_BrandMapper.selectByNameAndCityId(updateBrand.getCityId(), updateBrand.getBrandName());
		if (tb_Brand2 != null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_4, "该地区品牌已经存在");
		}
		tb_Brand1 = CopyerObject.copyerObject(updateBrand, TB_Brand.class);
		tb_Brand1.setUpdateTime(new Date());
		if (tb_BrandMapper.updateSelectiveByPrimaryKey(tb_Brand1) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_5, "修改品牌信息失败");
		}
	}

	/**
	 * 上下架品牌信息
	 * 
	 * @param upDownBrand
	 */
	@Transactional
	public void upDownBrand(RequestWebUpDownBrand upDownBrand) {
		CheckedUtil.isNull(upDownBrand.getBrandId(), Constants.REQUEST_ERROR_1, "请先选择要修改的品牌信息");
		TB_Brand tb_Brand = tb_BrandMapper.selectByPrimaryKey(upDownBrand.getBrandId());
		if (tb_Brand == null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_2, "要修改的品牌信息不存在或者已删除");
		}
		tb_Brand.setStatus(!tb_Brand.getStatus());
		tb_Brand.setUpdateTime(new Date());
		if (tb_BrandMapper.updateSelectiveByPrimaryKey(tb_Brand) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_3, "上下架品牌信息失败");
		}
	}

	/**
	 * web获取当前自己可用的品牌
	 * 
	 * @param webBrandSimple
	 * @return
	 */
	public List<ResponseWebBrandSimple> getBrandSimpleList(RequestWebBrandSimple webBrandSimple) {
		CheckedUtil.isNull(webBrandSimple.getCityId(), Constants.REQUEST_ERROR_1, "请先选择要修改的品牌信息");
		List<TB_Brand> tb_Brands = tb_BrandMapper.selectBrandList(false, null, webBrandSimple.getCityId());
		List<ResponseWebBrandSimple> brandSimples = new ArrayList<>();
		if (tb_Brands != null && tb_Brands.size() > 0) {
			tb_Brands.stream().forEach(r -> {
				ResponseWebBrandSimple brandSimple = CopyerObject.copyerObject(r, ResponseWebBrandSimple.class);
				brandSimple.setIdStr(r.getBrandId().toString());
				brandSimples.add(brandSimple);
			});
		}
		return brandSimples;
	}

	/**
	 * 获取可用品牌分类
	 * 
	 * @return
	 */
	public List<ResponseBrandSortsSimple> getBrandSortsSimpleList() {
		List<TB_BrandSorts> tb_BrandSorts = tb_BrandSortsMapper.getBrandSortsList(false, null);
		List<ResponseBrandSortsSimple> brandSortsSimples = new ArrayList<>();
		if (tb_BrandSorts != null && tb_BrandSorts.size() > 0) {
			tb_BrandSorts.stream().forEach(r -> {
				ResponseBrandSortsSimple brandSortsSimple = CopyerObject.copyerObject(r,
						ResponseBrandSortsSimple.class);
				brandSortsSimple.setIdStr(r.getSortsId().toString());
				brandSortsSimples.add(brandSortsSimple);
			});
		}
		return brandSortsSimples;

	}
}
